package com.foreveross.project.usercenterserver.oauth2.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.foreveross.project.usercenterserver.common.constant.ResultCode;
import com.foreveross.project.usercenterserver.common.utils.R;
import com.foreveross.project.usercenterserver.oauth2.domain.UcTenant;
import com.foreveross.project.usercenterserver.oauth2.dto.TenantDto;
import com.foreveross.project.usercenterserver.oauth2.service.IUcTenantService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author yz
 * @since 2019-07-19
 */
@Api(description = "租户管理")
@RestController
@RequestMapping("/oauth2/tenant")
public class UcTenantController {


    @Autowired
    private IUcTenantService ucTenantService;

    /**
     * 添加租户
     */
    @PostMapping(value = "/createTenant")
    @ApiOperation(value = "创建租户", notes = "成功返回 UcTenant 实体")
    public R createTenant(
            @Valid @RequestBody TenantDto tenantDto) {
        UcTenant tenant = null;
        try {
            tenant = ucTenantService.createTenant(tenantDto);
            return R.ok(tenant);
        } catch (Exception e) {
            //e.printStackTrace();
            return R.error(ResultCode.BAD_REQUEST.getCode(), e.getMessage());
        }
    }


    /**
     * 修改租户
     */
    @PostMapping(value = "/updateTenant")
    @ApiOperation(value = "修改租户", notes = "成功返回 UcTenant 实体")
    public R updateTenant(
            @Valid @RequestBody TenantDto tenantDto) {
        UcTenant tenant = null;
        try {
            tenant = ucTenantService.updateTenant(tenantDto);
            return R.ok(tenant);
        } catch (Exception e) {
            return R.error(ResultCode.BAD_REQUEST.getCode(), e.getMessage());
        }
    }


    /**
     * 删除租户
     */
    @DeleteMapping(value = "/deleteTenant/{tenantFkId}")
    @ApiOperation(value = "删除租户", notes = "成功返回code=200")
    public R deleteTenant(
            @PathVariable String tenantFkId
    ) {
        try {
            ucTenantService.deleteTenant(tenantFkId);
            return R.ok();
        } catch (Exception e) {
            return R.error(ResultCode.BAD_REQUEST.getCode(), e.getMessage());
        }
    }

    /**
     * 查询单个租户
     *
     * @param tenantFkId
     * @return
     */
    @GetMapping(value = "/detail/{tenantFkId}")
    @ApiOperation(value = "查询单个应用", notes = "成功返回UcTenant实体")
    @ApiImplicitParam(name = "tenantFkId", dataType = "String",  required = true, value = "租户fkid")
    public R tenantDetail(
            @PathVariable String tenantFkId
    ) {
        try {
            return R.ok(ucTenantService.findTenant(tenantFkId));
        } catch (Exception e) {
            return R.error(ResultCode.BAD_REQUEST.getCode(), e.getMessage());
        }
    }



    /**
     * 租户列表
     */
    @GetMapping(value = "/tenantList")
    @ApiOperation(value = "租户列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", dataType = "Integer", defaultValue = "1", required = false, value = "当前页码", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", dataType = "Integer", defaultValue = "10", required = false, value = "每页记录数", paramType = "query"),
            @ApiImplicitParam(name = "tenantName", dataType = "String", required = false, value = "租户名称", paramType = "query"),
            @ApiImplicitParam(name = "delFlag", dataType = "Integer", defaultValue = "0",required = false, value = "是否删除  -1：已删除  0：正常", paramType = "query"),
    })
    public R tenantList(
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(required = false, defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String tenantName,
            @RequestParam(required = false) Integer delFlag
    ) {
        //查询条件
        Map<String, Object> params = new HashMap<>();
        params.put("tenantName", tenantName);
        params.put("delFlag", delFlag);
        IPage<TenantDto> dtoIPage = ucTenantService.queryForPage(new Page<>(pageNum, pageSize), params);
        return R.ok(dtoIPage);
    }


    //修改用户密码



}
